package neusoft.edu.cn.guanshangshiguang.activities;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import neusoft.edu.cn.guanshangshiguang.Listener.ListListener;
import neusoft.edu.cn.guanshangshiguang.R;
import neusoft.edu.cn.guanshangshiguang.adapter.CommentSchoolAdapter;
import neusoft.edu.cn.guanshangshiguang.bean.CommentSchoolBean;
import neusoft.edu.cn.guanshangshiguang.common.Common;
import neusoft.edu.cn.guanshangshiguang.model.CommentSchoolModel;

public class SchoolBookDatailsActivity extends AppCompatActivity {
    private List<CommentSchoolBean> commentSchoolBean;
    CommentSchoolAdapter commentSchoolAdapter;
    private ImageView bpic;
    private TextView bname,borrow_time,jianjie,shujia,zaixianyuedu;
    private Button yuyue;
    private int shuaxin_flag=0;
    private int shujia_flag=0;
    private String isBorrow_flag;
    private RecyclerView contentRecycler;
    SwipeRefreshLayout swipeRefreshLayout;

    ListListener listListener = new ListListener() {
        @Override
        public void onResponse(List List) {
            commentSchoolBean = List;
            commentSchoolAdapter.setDate(commentSchoolBean);
            if(shuaxin_flag==1){
                shuaxin_flag=0;
                swipeRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();//去掉标题栏
        actionBar.hide();//去掉标题栏
        setContentView(R.layout.activity_school_book_datails);
        init();
        initRecycler();

        Picasso.get()
                .load(Common.Address+getIntent().getStringExtra("bpic"))
                .into(bpic);
        bname.setText(getIntent().getStringExtra("bname"));
        jianjie.setText("简介："+getIntent().getStringExtra("intro"));
        isBorrow_flag=getIntent().getStringExtra("isBorrow").toString();

        if(isBorrow_flag.equals("0")){
            borrow_time.setText("在架可借");
            yuyue.setText("预约");
            yuyue.setEnabled(true);
        }else {
            borrow_time.setText("已借出");
            yuyue.setText("不可预约");
            yuyue.setEnabled(false);
        }

        yuyue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                yuyue.setText("已预约");
            }
        });

        CommentSchoolModel commentSchoolModel = new CommentSchoolModel();
        commentSchoolModel.getResultList(getIntent().getStringExtra("school_bid"),listListener);

        shujia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(shujia_flag==0){
                    shujia_flag=1;
                    shujia.setText("✔ 已加入");
                }else{
                    shujia_flag=0;
                    shujia.setText("+ 书架");
                }

            }
        });

        zaixianyuedu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Intent intent = new Intent(getApplicationContext(),ReadingActivity.class);
                intent.putExtra("content",getIntent().getStringExtra("content"));
                intent.putExtra("bname",getIntent().getStringExtra("bname"));
                startActivity(intent);
                finish();
            }
        });
    }

    private void init() {
        bname=findViewById(R.id.school_bname);
        borrow_time=findViewById(R.id.school_author);
        jianjie=findViewById(R.id.school_jianjie);
        bpic=findViewById(R.id.school_bpic);
        yuyue=findViewById(R.id.yuyue);
        shujia=findViewById(R.id.shujia);
        zaixianyuedu=findViewById(R.id.zaixianyuedu);
    }
    private void initRecycler() {
        commentSchoolAdapter = new CommentSchoolAdapter();

        contentRecycler = findViewById(R.id.contentList);

        //布局管理器  可以设置显示数据的方式为垂直或者水平
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        //每个item如果是确定高度，设置此项提高性能
        contentRecycler.setHasFixedSize(true);

        swipeRefreshLayout = findViewById(R.id.swipeRefresh);

        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary),
                getResources().getColor(R.color.colorAccent),
                getResources().getColor(R.color.colorPrimaryDark));

        swipeRefreshLayout.setProgressViewOffset(true,50,60);

        swipeRefreshLayout.setRefreshing(false);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                shuaxin_flag=1;
                CommentSchoolModel commentSchoolModel = new CommentSchoolModel();
                commentSchoolModel.getResultList(getIntent().getStringExtra("school_bid"),listListener);
            }
        });

        contentRecycler.setLayoutManager(linearLayoutManager);
        contentRecycler.setAdapter(commentSchoolAdapter);

    }
}
